package com.ldoublem.loadingviewlib.view;




import ohos.agp.animation.Animator;
import ohos.agp.animation.AnimatorValue;
import ohos.agp.components.AttrSet;
import ohos.agp.components.Component;
import ohos.agp.render.Arc;
import ohos.agp.render.Canvas;
import ohos.agp.render.Paint;
import ohos.agp.utils.Color;
import ohos.agp.utils.RectFloat;
import ohos.app.Context;

import com.ldoublem.loadingviewlib.view.base.LVBase;



public class LVWifi extends LVBase implements Component.EstimateSizeListener, Component.DrawTask {
    private float mWidth = 0f;
    private float mPadding = 0f;
    private Paint mPaint;
    private int signalSize = 4;



    public LVWifi(Context context) {
        this(context, null);
    }

    public LVWifi(Context context, AttrSet attrs) {
        this(context, attrs, 0);
    }

    public LVWifi(Context context, AttrSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initPaint();
    }

    private void initPaint() {
        addDrawTask(this);
        setEstimateSizeListener(this);
        mPaint = new Paint();
        mPaint.setAntiAlias(true);
        mPaint.setStyle(Paint.Style.STROKE_STYLE);
        mPaint.setColor(Color.WHITE);


    }

    public void setViewColor(int color)
    {
        mPaint.setColor(new Color(color));
        invalidate();
    }

    private float mAnimatedValue = 0.9f;

    @Override
    protected void InitPaint() {

    }

    @Override
    protected void OnAnimationUpdate(AnimatorValue valueAnimator, float v) {
        mAnimatedValue = v;
        invalidate();
    }

    @Override
    protected void OnAnimationRepeat(Animator animation) {

    }

    @Override
    protected int OnStopAnim() {
        mAnimatedValue = 0.9f;
        invalidate();
        return 1;
    }

    @Override
    protected int SetAnimRepeatMode() {
        return RESTART;
    }

    @Override
    protected void AinmIsRunning() {

    } @Override
    protected int SetAnimRepeatCount() {
        return INFINITE;
    }


    @Override
    public void onDraw(Component component, Canvas canvas) {
        canvas.save();
        canvas.translate(0, mWidth / signalSize);
        mPaint.setStrokeWidth(mWidth / signalSize / 2 / 2 / 2);
        int scale = (int) ((mAnimatedValue * signalSize - (int) (mAnimatedValue * signalSize)) * signalSize)+1;
        RectFloat rect = null;
        float signalRadius = mWidth / 2 / signalSize;
        for (int i = 0; i < signalSize; i++) {

            if (i>=signalSize-scale) {
                float radius = signalRadius * i;
                rect = new RectFloat(
                        radius,
                        radius,
                        mWidth - radius,
                        mWidth - radius);
                if (i < signalSize - 1) {
                    mPaint.setStyle(Paint.Style.STROKE_STYLE);
                    canvas.drawArc(rect, new Arc(-135, 90
                            , false), mPaint);
                } else {
                    mPaint.setStyle(Paint.Style.FILL_STYLE);
                    canvas.drawArc(rect, new Arc(-135, 90
                            , true) , mPaint);
                }
            }

        }
        canvas.restore();
    }

    @Override
    public boolean onEstimateSize(int i, int i1) {

        if (EstimateSpec.getSize(i)> getHeight())
            mWidth = EstimateSpec.getSize(i1);
        else
            mWidth = EstimateSpec.getSize(i) ;
        mPadding = dip2px(1);
        return false;
    }
}
